package com.scsj.supermarket.view.fragment.goodsdetailmodel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.GoodsCommAdapter;
import com.scsj.supermarket.adapter.GoodsCommTypeAdapter;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.activity.goodmodel.GoodsDetailActivity;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.library.flowlayout.FlowLayoutManager;
import com.library.flowlayout.SpaceItemDecoration;
import com.vondear.rxtools.RxDeviceTool;
import com.vondear.rxtools.RxImageTool;

import java.util.ArrayList;
import java.util.List;

/**
 * item页ViewPager里的评价Fragment
 */
public class GoodsCommentFragment extends BaseFragment implements View.OnClickListener{
    public TextView tv_comment_count, tv_good_comment;
    public GoodsDetailActivity activity;
    //只看当前商品
    private Boolean isOnlyCurrent = false;//是否只看当前商品
    private LinearLayout only_current_ll;
    private ImageView only_current_iv;

    private LinearLayout ll_empty_comment;

    private RecyclerView header_recycler;
    private GoodsCommTypeAdapter goodsCommTypeAdapter;
    private RecyclerView recycler_view;
    private GoodsCommAdapter mAdapter;
    private List<String> mDatas;

//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        this.activity = (GoodsDetailActivity) context;
//    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_goods_comment, null);
        initView(rootView);
        initListener();
        initData();
        return rootView;
    }

    private void initView(View rootView) {
        //只看当前商品
        only_current_ll = rootView.findViewById(R.id.only_current_ll);
        only_current_iv = rootView.findViewById(R.id.only_current_iv);

        tv_comment_count = rootView.findViewById(R.id.tv_comment_count);
        tv_good_comment = rootView.findViewById(R.id.tv_good_comment);
        ll_empty_comment = rootView.findViewById(R.id.ll_empty_comment);
        ll_empty_comment.setVisibility(View.GONE);

        recycler_view = rootView.findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new GoodsCommAdapter(getContext(), getExample("评价", 10));
        recycler_view.setAdapter(mAdapter);

        View headerView = getLayoutInflater().inflate(R.layout.fragment_goods_comment_header, null);
        headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        header_recycler = headerView.findViewById(R.id.recycler_view);
        final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) header_recycler.getLayoutParams();
        lp.width = RxDeviceTool.getScreenWidths(getContext());
        final FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        header_recycler.addItemDecoration(new SpaceItemDecoration(RxImageTool.dp2px(5)));
        header_recycler.setLayoutManager(flowLayoutManager);
        goodsCommTypeAdapter = new GoodsCommTypeAdapter(getContext(), getExample1());
        header_recycler.setAdapter(goodsCommTypeAdapter);
        mAdapter.addHeaderView(headerView);
        header_recycler.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                header_recycler.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                lp.height = flowLayoutManager.getTotalHeight() + header_recycler.getPaddingBottom() + header_recycler.getPaddingTop();
                header_recycler.setLayoutParams(lp);
            }
        });
//        mAdapter.addData();
    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {

    }

    protected void initListener() {
        only_current_ll.setOnClickListener(this);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SkipUtils.toGoodCommDetail(getContext());
            }
        });
    }

    protected void initData() {


    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.only_current_ll://只看当前商品
                if(isOnlyCurrent){
                    isOnlyCurrent = false;
                    only_current_iv.setImageResource(R.mipmap.goods_comm_unselected);
                }else {
                    isOnlyCurrent = true;
                    only_current_iv.setImageResource(R.mipmap.goods_comm_selected);
                }
                break;
        }
    }


    public static List<String> getExample(String str, int num) {
        List<String> bannerList = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
            bannerList.add(str + i);
        }
        return bannerList;
    }

    public static List<String> getExample1() {
        List<String> bannerList = new ArrayList<>();
        bannerList.add("全部 11万");
        bannerList.add("好评 10.6万");
        bannerList.add("中评 2710");
        bannerList.add("差评 1751");
        bannerList.add("有图 1万");
        bannerList.add("追加评价 325");
        return bannerList;
    }
}
